package com.lego.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lego.service.GoodsService;
import com.lego.service.OrdersService;
import com.lego.service.impl.GoodsServiceImpl;
import com.lego.service.impl.OrdersServiceImpl;

@WebServlet("/changeStatus.do")
public class ChangeStatusServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		OrdersService ods = new OrdersServiceImpl();
		GoodsService gds = new GoodsServiceImpl();
		request.setCharacterEncoding("utf-8");
		String receivingAddress = request.getParameter("receivingAddress");
		String orderNumber = null;
		try {
			orderNumber = URLDecoder.decode(request.getParameter("orderNumber"), "UTF-8");
		} catch (Exception e) {
			System.out.println("没有传入订单编号");
		}
		String orderStatus = request.getParameter("orderStatus");
		int goodsId = 0;
		int totalQuantity = 0;
		try {
			goodsId = Integer.parseInt(request.getParameter("goodsId"));
		} catch (Exception e) {
			System.out.println("没有查询到该商品信息");
		}
		try {
			totalQuantity = Integer.parseInt(request.getParameter("totalQuantity"));
		} catch (Exception e) {
			System.out.println("没有查询到该商品信息");
		}
		int negativeNumber = -totalQuantity;
		if(goodsId!=0&&totalQuantity!=0) {
			gds.changeQuantity(negativeNumber,goodsId);
		}
		int row1 = ods.updateStatus(orderStatus,orderNumber,receivingAddress);
		if(row1!=1) {
			System.out.println("修改订单状态失败！");
		}
		System.out.println(orderStatus);
		if(orderStatus.equals("toBeReviewed")) {
			request.getRequestDispatcher("personal.jsp").forward(request, response);
		}
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
